<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\ServiceModel;
use cmf\controller\AdminBaseController;
use think\Db;
use app\portal\model\PortalPostModel;

class AdminServiceController extends AdminBaseController
{
    //主要服务分类


    //列表
    public function index(){
        $list = Db::name('service')
            ->select()
            ->toArray();
        $this->assign('list',$list);
        return $this->fetch();
    }

    //添加页面
    public function add(){
        return $this->fetch();
    }

    //添加提交
    public function addPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'AdminService.edit');
        if($result !== true){
            $this->error($result);
        }

        $serviceModel = new ServiceModel();
        $res = $serviceModel->create($data);
        if($res){
            $this->success('添加成功!', url('AdminService/index'));
        }else{
            $this->error('失败');
        }
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id');
        //内容
        $post = Db::name('service')
            ->where(['id'=>$id])
            ->find();
        $contentModel = new PortalPostModel();
        $post['detail'] = $contentModel->getPostContentAttr($post['detail']);
        $post['detail_en'] = $contentModel->getPostContentAttr($post['detail_en']);
        $this->assign('post',$post);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'AdminService.edit');
        if($result !== true){
            $this->error($result);
        }

        $serviceModel = new ServiceModel();
        $contentModel = new PortalPostModel();
        $data['detail'] = $contentModel->setPostContentAttr($data['detail']);
        $data['detail_en'] = $contentModel->setPostContentAttr($data['detail_en']);
        // 显式指定更新数据操作
        $res = $serviceModel->isUpdate(true)->save($data);
        if($res){
            $this->success('保存成功!', url('AdminService/index'));
        }else{
            $this->error('失败');
        }
    }

    //删除
    public function delete(){
        $id = $this->request->param('id');
        $serviceModel = new ServiceModel();
        $res = $serviceModel->where(['id'=>$id])->delete();
        if($res){
            $this->success('删除成功!');
        }else{
            $this->error('失败');
        }
    }
}
